package com.siecomzg.transport.handler;

import android.graphics.BitmapFactory;
import android.os.Bundle;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.google.protobuf.ByteString;
import com.siecomzg.constconfig.ConstDefine;
import com.siecomzg.proto.Message;
import com.siecomzg.tool.LogUtil;
import com.siecomzg.tool.Point;
import com.siecomzg.transport.CallbackMap;
import com.siecomzg.transport.CallbackTask;
import com.siecomzg.transport.CommonClient;
import com.siecomzg.transport.DeviceCallback;
import com.siecomzg.transport.SiecomDevice;

import java.util.ArrayList;
import java.util.HashMap;

/**
 * Created on 2017/2/6.
 */

public class SignatureHandler extends CommonHandler {
    private final static String TAG = "SignatureHandler";

    @Override
    public void onReceive(CommonClient server, Message.siecomproto msg) {
        String msgid = msg.getMsgid();
        HashMap<String, CallbackTask> callbackTaskHashMap = CallbackMap.getCallbackTaskHashMap();
        if(callbackTaskHashMap.containsKey(msgid)){
            CallbackTask callbackTask = callbackTaskHashMap.get(msgid);
            DeviceCallback callback = callbackTask.getCallback();
            callbackTask.getTimer().cancel();
            boolean isOnMain = callbackTask.isOnMain();
            int respondCmd = msg.getCmd();
            LogUtil.e(TAG,"respondCmd = " + respondCmd);
            if(respondCmd== ConstDefine.SIGNATURE_RESPOND){

                readSignatureByRemote(msg,callback, isOnMain);
            }
            if(respondCmd==ConstDefine.SIGNATURE_CANCEL_RESPOND){

                cancelCall(msg,callback, isOnMain);
                callbackTaskHashMap.remove(msgid);
            }

            if(respondCmd==ConstDefine.SIGNATURE_SM2_RESPOND){

                transSM2PublicKey(msg,callback,isOnMain);
                callbackTaskHashMap.remove(msgid);

            }
            if(respondCmd==ConstDefine.SIGNATURE_SM4_RESPOND){

                setSM4PublicKey(msg,callback,isOnMain);
                callbackTaskHashMap.remove(msgid);
            }

        }
    }
    private void setSM4PublicKey(Message.siecomproto msg, DeviceCallback callback, boolean isOnMain) {

        int errno = msg.getError();
        if (errno == 0) {
            Bundle bundle = new Bundle();
            bundle.putString("data",msg.getRespond());
            SiecomDevice.deliverResult(callback, SiecomDevice.Result.SUCCEED, bundle, isOnMain);
        } else {
            Bundle bundle = new Bundle();
            bundle.putInt("code", errno);
            bundle.putString("message", msg.getRespond());
            SiecomDevice.deliverResult(callback, SiecomDevice.Result.ERROR, bundle, isOnMain);

        }
        SiecomDevice.deliverResult(callback, SiecomDevice.Result.COMPLETED, new Bundle(), isOnMain);

    }
    private void transSM2PublicKey(Message.siecomproto msg, DeviceCallback callback, boolean isOnMain) {

        int errno = msg.getError();
        if (errno == 0) {
            Bundle bundle = new Bundle();
            bundle.putString("data",msg.getRespond());
            SiecomDevice.deliverResult(callback, SiecomDevice.Result.SUCCEED, bundle, isOnMain);
        } else {
            Bundle bundle = new Bundle();
            bundle.putInt("code", errno);
            bundle.putString("message", msg.getRespond());
            SiecomDevice.deliverResult(callback, SiecomDevice.Result.ERROR, bundle, isOnMain);

        }
        SiecomDevice.deliverResult(callback, SiecomDevice.Result.COMPLETED, new Bundle(), isOnMain);

    }




    private void readSignatureByRemote(Message.siecomproto msg,DeviceCallback callback,boolean isOnMain) {
        HashMap<String, CallbackTask> callbackTaskHashMap = CallbackMap.getCallbackTaskHashMap();
//        LogUtil.e("readSignatureByRemote",msg.toString());
        int errno = msg.getError();
        if (errno == 0) {
            if(msg.getRespond()!=null&&msg.getRespond().equals("sign")){
                Bundle bundle = new Bundle();
                ByteString byteString = msg.getExtion();
                byte[] bytes = byteString.toByteArray();
                bundle.putByteArray("data",bytes);
                SiecomDevice.deliverResult(callback, SiecomDevice.Result.COMPLETED, bundle, isOnMain);
//                callbackTaskHashMap.remove(msg.getMsgid());

            }else if(msg.getRespond()!=null&&msg.getRespond().equals("UnencryptedTrack")){
                Bundle bundle = new Bundle();
//                ByteString byteString = msg.getExtion();
//                byte[] pointByte = byteString.toByteArray();
//                bundle.putParcelableArrayList("NoPasswordTrack",(ArrayList) ByteTool.getInfoListFromBytes(pointByte));
                String p = msg.getRequest();
                ArrayList<Point> points = new Gson().fromJson(p,new TypeToken<ArrayList<Point>>(){}.getType());
                bundle.putParcelableArrayList("NoPasswordTrack",  (ArrayList)points);
                SiecomDevice.deliverResult(callback, SiecomDevice.Result.COMPLETED, bundle, isOnMain);
//                callbackTaskHashMap.remove(msg.getMsgid());

            }else {
                Bundle bundle = new Bundle();
                ByteString byteString = msg.getExtion();
                ByteString pointString = msg.getMsgidBytes();
                byte[] pointByte = pointString.toByteArray();
                byte[] bitmapByte = byteString.toByteArray();
                LogUtil.e(TAG,"bitmapByte.length = " + bitmapByte.length);
                bundle.putParcelable("data", BitmapFactory.decodeByteArray(bitmapByte, 0, bitmapByte.length));
                bundle.putString("track",msg.getRespond());
                bundle.putParcelable("bitmap", BitmapFactory.decodeByteArray(bitmapByte, 0, bitmapByte.length));
                bundle.putString("spotStr",msg.getRespond());
                SiecomDevice.deliverResult(callback, SiecomDevice.Result.SUCCEED, bundle, isOnMain);
            }
        } else {
            Bundle bundle = new Bundle();
            bundle.putInt("code", errno);
            bundle.putString("message", msg.getRespond());
            SiecomDevice.deliverResult(callback, SiecomDevice.Result.ERROR, bundle, isOnMain);
            callbackTaskHashMap.remove(msg.getMsgid());

        }


    }


    private void cancelCall(Message.siecomproto msg,DeviceCallback callback,boolean isOnMain) {
        int errno = msg.getError();
        if (errno == 0) {
            Bundle bundle = new Bundle();
            bundle.putString("data",msg.getRespond());
            SiecomDevice.deliverResult(callback, SiecomDevice.Result.SUCCEED, bundle, isOnMain);
        } else {
            Bundle bundle = new Bundle();
            bundle.putInt("code", errno);
            bundle.putString("message", msg.getRespond());
            SiecomDevice.deliverResult(callback, SiecomDevice.Result.ERROR, bundle, isOnMain);

        }
        SiecomDevice.deliverResult(callback, SiecomDevice.Result.COMPLETED, new Bundle(), isOnMain);
    }


}
